<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {

  var $songId;

  function PageController() {
  	parent::__construct();
	$this->title = "Audio";
	$this->section = "songs";

	$action = $this->getParameter("action");

	switch (strtoupper($action))
	{
		case "LOAD" :
			$this->parseForm();
			$song = SongLogic::load($this->songId);

			if (strlen($song->audio) > 0)
			{
				if (strpos($song->audio, "http://") === 0)
				{
					header("Location: $song->audio");
					exit;
				}
				$audioFile = SongLogic::getSongDir() . $song->audio;

				if (file_exists($audioFile))
				{
					header("Content-type: audio/mpeg");
					header('Content-Transfer-Encoding: Binary');
					header("Content-length: ".filesize($audioFile));
					header("Content-Disposition: attachment; filename=". $song->audio);
					readfile($audioFile);
					exit;
				}
			}

			// Couldn't do something right, so redirect them
			$this->addError("Couldn't read the file, so you don't get to have it.  Sorry.");
			break;
	}

  }

  function parseForm()
  {
  	 $this->songId = $this->getParameter("id");
  }
}



?>
